package com.hake.manager.system.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hake.core.exception.AppException;
import com.hake.core.support.HttpCode;
import com.hake.core.support.web.BaseController;
import com.hake.core.util.RequestParamUtil;
import com.hake.manager.system.entity.Menu;
import com.hake.manager.system.service.MenuService;

@Controller
@RequestMapping("/manager/system/menu")
public class MenuController extends BaseController {

	@Autowired
	private MenuService service ;
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main() {
		return viewName("main") ;
	}
	
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Integer id, ModelMap modelMap) {
		if(null != id) {
			modelMap.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public Object add(HttpServletRequest request, ModelMap modelMap) {
		try {
			if(this.service.insertSelective(RequestParamUtil.covert(Menu.class, request)) > 0) {
				return setModelMap(modelMap, HttpCode.ADD_SUCCESS) ;
			} else {
				return setModelMap(modelMap, HttpCode.ADD_FAILED) ;
			}
		} catch (AppException e) {
			return setModelMap(modelMap, HttpCode.INTERNAL_SERVER_ERROR) ;
		}
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public Object update(HttpServletRequest request, ModelMap modelMap) {
		try {
			if(this.service.updateByPrimaryKeySelective(RequestParamUtil.covert(Menu.class, request)) > 0) {
				return setModelMap(modelMap, HttpCode.UPDATE_SUCCESS) ;
			} else {
				return setModelMap(modelMap, HttpCode.UPDATE_FAILED) ;
			}
		} catch (AppException e) {
			return setModelMap(modelMap, HttpCode.INTERNAL_SERVER_ERROR) ;
		}
		
	}
	
	@RequestMapping(value="/{id}/get", method=RequestMethod.GET)
	@ResponseBody
	public Object get(@PathVariable Integer id, ModelMap modelMap) {
		try {
			Menu data = this.service.selectByPrimaryKey(id) ;
			if(data != null) {
				return setSuccessModelMap(modelMap, data) ;
			} else {
				return setModelMap(modelMap, HttpCode.UPDATE_FAILED) ;
			}
		} catch (AppException e) {
			return setModelMap(modelMap, HttpCode.INTERNAL_SERVER_ERROR) ;
		}
	}
	
	@RequestMapping(value="/{id}/delete", method=RequestMethod.DELETE)
	@ResponseBody
	public Object delete(@PathVariable Integer id, ModelMap modelMap) {
		try {
			if(this.service.deleteByPrimaryKey(id) > 0) {
				return setModelMap(modelMap, HttpCode.DELETE_SUCCESS) ;
			} else {
				return setModelMap(modelMap, HttpCode.DELETE_FAILED) ;
			}
		} catch (AppException e) {
			return setModelMap(modelMap, HttpCode.INTERNAL_SERVER_ERROR) ;
		}
	}
	
	
	@RequestMapping(value="/selectMenuByCategory", method=RequestMethod.GET)
	@ResponseBody
	public List<Menu> selectMenuByCategory(HttpServletRequest request) throws AppException {
		try {
			return this.service.selectMenuByCategory() ;
		} catch (AppException e) {
			throw e;
		}
	}
	
	@RequestMapping(value="/selectMenuByCategoryForCache", method=RequestMethod.GET)
	@ResponseBody
	public List<Menu> selectMenuByCategoryForCache(HttpServletRequest request) throws AppException {
		try {
			return this.service.selectMenuByCategoryForCache() ;
		} catch (AppException e) {
			throw e;
		}
	}
	
	@RequestMapping(value="/{parentId}/selectMenuByParentId", method=RequestMethod.GET)
	@ResponseBody
	public List<Menu> selectMenuByParentId(@PathVariable Integer parentId) throws AppException {
		try {
			return this.service.selectMenuByParentId(parentId) ;
		} catch (AppException e) {
			throw e;
		}
	}
	
	@RequestMapping(value="/{parentId}/selectMenuByParentIdForCache", method=RequestMethod.GET)
	@ResponseBody
	public List<Menu> selectMenuByParentIdForCache(@PathVariable Integer parentId) throws AppException {
		try {
			return this.service.selectMenuByParentIdForCache(parentId) ;
		} catch (AppException e) {
			throw e;
		}
	}
	
	@RequestMapping(value="/treegrid")
	@ResponseBody
	public List<Menu> treegrid() throws AppException {
		try {
			return this.service.treegrid() ;
		} catch (AppException e) {
			throw e;
		}
	}
	
	@RequestMapping(value="/treegridForCache")
	@ResponseBody
	public List<Menu> treegridForCache() throws AppException {
		try {
			return this.service.treegridForCache() ;
		} catch (AppException e) {
			throw e;
		}
	}
	
	@RequestMapping(value="/getLocation", method=RequestMethod.GET)
	public @ResponseBody Menu getLocation(String location) {
		return this.service.selectMenuByLocation(location) ;
	}
	
	@RequestMapping(value="/getLocationForCache", method=RequestMethod.GET)
	public @ResponseBody Menu getLocationForCache(String location) {
		return this.service.selectMenuByLocationForCache(location) ;
	}
	
}

